package repository

import (
	"context"
	"webook/pkg/logger"
	"webook/search/domain"
	"webook/search/repository/dao"
)

type userSearchRepository struct {
	dao dao.UserDAO
	l   logger.LoggerV1
}

func NewUserSearchRepository(dao dao.UserDAO, l logger.LoggerV1) UserSearchRepository {
	return &userSearchRepository{
		dao: dao,
		l:   l,
	}
}

func (repo *userSearchRepository) InputUser(ctx context.Context, msg domain.User) error {
	return repo.dao.InputUser(ctx, repo.domainToEntity(msg))
}

func (repo *userSearchRepository) SearchUser(ctx context.Context, expression []string) ([]domain.User, error) {
	users, err := repo.dao.Search(ctx, expression)
	if err != nil {
		return []domain.User{}, err
	}

	res := make([]domain.User, 0, len(users))
	for _, user := range users {
		res = append(res, repo.entityToDomain(user))
	}

	return res, nil
}

func (repo *userSearchRepository) domainToEntity(user domain.User) dao.User {
	return dao.User{
		Id:       user.Id,
		Email:    user.Email,
		Nickname: user.Nickname,
		Phone:    user.Phone,
	}
}

func (repo *userSearchRepository) entityToDomain(user dao.User) domain.User {
	return domain.User{
		Id:       user.Id,
		Email:    user.Email,
		Nickname: user.Nickname,
		Phone:    user.Phone,
	}
}
